//
//  JXEmoji.m
//  Emoji_keyboard
//
//  Created by raymond on 16/4/15.
//  Copyright © 2016年 raymond. All rights reserved.
//

#import "JXEmoji.h"

/// 表情包路径
#define BundlePath                                                             \
  [[NSBundle mainBundle] pathForResource:@"Emoji" ofType:@"bundle"]

#pragma mark - 表情包模型

static NSArray *_packages;

@interface JXEmojiPackage ()

@property (nonatomic, copy) NSString *id;
@property (nonatomic, copy) NSString *packageName; // 表情包名称

@end

@implementation JXEmojiPackage

+ (NSArray *)packages {
    if (!_packages.count) {
        [self loadPackages];
    }
    return _packages;
}

+ (NSArray *)loadPackages {
    NSString *plistPath = [BundlePath stringByAppendingPathComponent:@"emoticons.plist"];
    NSDictionary *emoticon = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    NSArray *packages = emoticon[@"packages"];
    NSMutableArray *emojiPackages = [NSMutableArray array];
    for (NSDictionary *dict in packages) {
        NSString *id = dict[@"id"];
        JXEmojiPackage *package = [[JXEmojiPackage alloc] initWithId:id];
        [package loadEmoji];
        [emojiPackages addObject:package];
    }
    _packages = emojiPackages;
    return emojiPackages;
}

- (instancetype)initWithId:(NSString *)id {
    if (self = [super init]) {
        _id = id;
    }
    return self;
}

- (void)loadEmoji {
    if (!self.id.length) {
        return ;
    }
    NSString *path = [BundlePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@.plist", self.id, self.id]];
    NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
    NSMutableArray *emojiArr = [NSMutableArray arrayWithCapacity:dictArray.count];
    int index = 0;
    for (NSDictionary *dict in dictArray) {
        if (index == 20) {
            JXEmoji *removeEmoji = [[JXEmoji alloc] initWith:YES];
            [emojiArr addObject:removeEmoji];
            index = 0;
        }
        JXEmoji *emoji = [[JXEmoji alloc] initWithId:self.id andDict:dict];
        [emojiArr addObject:emoji];
        index ++;
    }
    self.emoticons = emojiArr;
}

@end

#pragma mark -
#pragma mark - 表情模型

@interface JXEmoji ()

@property(nonatomic, copy) NSString *bundlePath;

@end

@implementation JXEmoji

- (instancetype)initWithId:(NSString *)id andDict:(NSDictionary *)dict {
    if (self = [super init]) {
        if (id) {
            _id = id;
            _bundlePath = [BundlePath stringByAppendingPathComponent:id];
        } else {
            _bundlePath = BundlePath;
        }
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

- (instancetype)initWith:(BOOL)isRemoveButton {
    if (self = [super init]) {
        _isRemoveButton = isRemoveButton;
    }
    return self;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
}

- (void)setValue:(id)value forKey:(NSString *)key {
  if ([key isEqualToString:@"png"]) {
    _pngPath = [self.bundlePath stringByAppendingPathComponent:value];
  }
  [super setValue:value forKey:key];
}

@end
